package com.source.multithread.hander;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


class ReadXmlFile {
	private static Logger ILogger = LoggerFactory.getLogger(ReadXmlFile.class);

	//定义一个List集合，保存xml文档信息。
	private static List<Map<String, Map<String, String>>> list = new ArrayList<Map<String,Map<String, String>>>();
	
	/**
	 * 读取xml文件的节点属性
	 * @param xmlFile
	 * @return
	 */
	protected static Map<String, List<Map<String, String>>> readXmlFileAttr(File xmlFile){
		Map<String, List<Map<String, String>>> xmlInfoMap = new HashMap<String, List<Map<String,String>>>();
		//查询请求基本数据项
		List<Map<String, String>> basicList = new ArrayList<Map<String,String>>();
		//查询人数据项
		List<Map<String, String>> queryPersonList = new ArrayList<Map<String,String>>();
		//查询主体数据项
		List<Map<String, String>> queryMainList = new ArrayList<Map<String,String>>();
		//法律文书信息内容
		List<Map<String, String>> wsinfoList = new ArrayList<Map<String,String>>();
		//文件类型
		String sortType = DictCodeSZGA.getReqSortType(xmlFile.getName());
		String rootElementName = "";
		try {
			//读取xml文件的节点属性
			SAXReader xmlRead = new SAXReader();
			Document document = xmlRead.read(xmlFile);
			Element rootElement  = document.getRootElement();
			rootElementName = rootElement.getName();
			//获取根节点下所有子节点的属性信息
			getChildElementAttributes(rootElement, rootElementName);
			//读取内容成功，将读取到的内容进行转换
			for (int i = 0; i < list.size(); i++) {
				Map<String, Map<String, String>> nodeMap = list.get(i);
				Iterator<String> itKeys = nodeMap.keySet().iterator();
				while(itKeys.hasNext()){
					String currKeyName = itKeys.next();//当前节点的属性集合map的key
					Map<String, String> attributeMap = nodeMap.get(currKeyName);
					attributeMap.put("lineId", sortType + currKeyName.substring(currKeyName.length()-7, currKeyName.length()));//SS010000001
					//判断内容，添加至指定的list
					if(currKeyName.startsWith("QUERYFORM/BASICINFO")){//查询请求基本数据项
						basicList.add(attributeMap);
					}else if(currKeyName.startsWith("QUERYFORM/QUERYPERSON")){//查询人数据项
						queryPersonList.add(attributeMap);
					}else if(currKeyName.startsWith("QUERYFORM/LIINFOS/WSINFO")){//法律文书信息内容
						wsinfoList.add(attributeMap);
					}else{
						String cxnr = attributeMap.get("CXNR");//查询内容
						if("01".equals(cxnr)){
							if(currKeyName.startsWith("QUERYFORM/QUERYMAINS/QUERYMAIN")){
								//1、常规查询-根据证照号查询账户信息
								attributeMap.put("queryCondition", SZGAEnums.A.getCode());
							}else if(currKeyName.startsWith("QUERYFORM/QUERYACCOUNTS/QUERYACCOUNT")){
								//2、常规查询-根据账卡号查询账户信息
								attributeMap.put("queryCondition", SZGAEnums.B.getCode());
							}
						}else if("02".equals(cxnr)){
							//3、常规查询-根据账卡号查询交易明细信息
							attributeMap.put("queryCondition", SZGAEnums.D.getCode());
						}else if("03".equals(cxnr)){
							if(currKeyName.startsWith("QUERYFORM/QUERYMAINS/QUERYMAIN")){
								//4、常规查询-根据证照号---查询账户信息和交易明细信息
								attributeMap.put("queryCondition", SZGAEnums.E.getCode());
							}else if(currKeyName.startsWith("QUERYFORM/QUERYACCOUNTS/QUERYACCOUNT")){
								//5、常规查询-根据账卡号---查询账户信息和交易明细信息
								attributeMap.put("queryCondition", SZGAEnums.F.getCode());
							}
						}
						queryMainList.add(attributeMap);
					}
					//从这个集合中移除，释放内存
					itKeys.remove();
				}
			}
			//添加至集合，返回信息
			xmlInfoMap.put("basicList", basicList);
			xmlInfoMap.put("queryPersonList", queryPersonList);
			xmlInfoMap.put("queryMainList", queryMainList);
			xmlInfoMap.put("wsinfoList", wsinfoList);
		} catch (Exception e) {

		}
		return xmlInfoMap;
	}
	
	/**
	 * 读取xml文件节点属性内容
	 * @param currentElement 当前节点对象
	 * @param currentNodeName 当前节点名
	 * @throws Exception
	 */
	private static void getChildElementAttributes(Element currentElement,String currentNodeName) throws Exception{
		Iterator<?> rootIt = currentElement.elementIterator();
		int currCount = 0;
		while(rootIt.hasNext()){
			//定义一个节点的map，保存当前节点的属性
			Map<String, Map<String, String>> elementMap = new HashMap<String, Map<String,String>>();
			Element childElement = (Element)rootIt.next();
			Iterator<?> childIt = childElement.elementIterator();
			if(childIt.hasNext()){
				getChildElementAttributes(childElement, (currentNodeName + "/" + childElement.getName()));
			}else{
				currCount += 1;
				String childElementName = childElement.getName();
				String currKeyName = currentNodeName + "/" + childElementName + StringUtils.leftPad(Integer.toString(currCount), 7, "0");
				//获取当前子节点的属性--创建一个Map保存这个节点的属性
				Map<String, String> attributeMap = new HashMap<String, String>();
				@SuppressWarnings("unchecked")
				List<Attribute> attributes = childElement.attributes();
				for(int i=0;i<attributes.size();i++){
					Attribute attribute = attributes.get(i);
					attributeMap.put(attribute.getName(), attribute.getValue());
				}
				//将这个节点的属性attributesMap集合添加至一个节点Map当前节点+当前节点的序号做为key
				elementMap.put(currKeyName, attributeMap);
			}
			//将这个节点添加至list集合
			list.add(elementMap);
		}
	}
}
